package com.personal.cuncunxing.module.home;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.personal.cuncunxing.R;
import com.personal.cuncunxing.common.OkHttpUtils;
import com.personal.cuncunxing.common.ResultCallback;
import com.personal.cuncunxing.common.StaticFiled;
import com.personal.cuncunxing.module.home.live.LiveFragment;
import com.personal.cuncunxing.module.home.pojo.ContactBeanConvert;
import com.personal.cuncunxing.module.home.pojo.Movie;
import com.personal.cuncunxing.module.home.recommend.RecommendFragment;
import com.personal.cuncunxing.module.home.sort.SortFragment;
import com.personal.cuncunxing.util.BarUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private String[] title = {"推荐", "电影", "电视剧", "动漫", "直播"};
    private RecyclerView recycler_view;
    private ViewPager viewPager;
    private List<Fragment> lists;
    private FragmentAdapter fragmentAdapter;
    private TabRVAdapter tabAdapter;
    private LiveFragment liveFragment;
    private RecommendFragment recommendFragment;
    private boolean isPlayed;//标记是否播放过电视地址
    private Context context;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = getBaseContext();
        BarUtils.transparentStatusBar(MainActivity.this);
        BarUtils.setNavBarColor(this, getResources().getColor(R.color.colorHome_bg));
        recycler_view = findViewById(R.id.home_tab_rv);
        viewPager = findViewById(R.id.home_tab_vp);
        //设置瀑布流布局管理器
        StaggeredGridLayoutManager layoutManager = new
                StaggeredGridLayoutManager(title.length, StaggeredGridLayoutManager.VERTICAL);//表示每行size()列，垂直方向
        recycler_view.setLayoutManager(layoutManager);
        //设置适配器
        tabAdapter = new TabRVAdapter(this, title);
        recycler_view.setAdapter(tabAdapter);
        //添加碎片
        addFragment();
        //监听viewpager
        listenViewPager();
        //监听Tab页面点击切换
        listenItemClick();
    }

    private void listenItemClick() {
        tabAdapter.setOnItemClickListener(new TabRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.d(TAG, "onItemClick: " + position);
                viewPager.setCurrentItem(position);
            }
        });
    }


    private void addFragment() {
        lists = new ArrayList<>();
        liveFragment = new LiveFragment();
        recommendFragment = new RecommendFragment(viewPager);
        lists.add(recommendFragment);//推荐
        lists.add(new SortFragment());//电影
        lists.add(new SortFragment());//电视剧
        lists.add(new SortFragment());//动漫
        lists.add(liveFragment);//直播
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), 0);
        fragmentAdapter.setFragmentList(lists);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setCurrentItem(0, false);
    }

    private void listenViewPager() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                tabAdapter.setSelected(i);
                if (i != 4 && liveFragment.getLiveVideo().isPlaying()) {
                    liveFragment.getLiveVideo().pause();
                    isPlayed = true;
                } else if (i == 4) {
                    if (isPlayed) {
                        liveFragment.getLiveVideo().start();
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}
